package com.pig4cloud.pigx.wallet.schedule;

import com.pig4cloud.pigx.common.customize.enums.CommonDictEnum;
import com.pig4cloud.pigx.common.customize.redis.constants.RedisKeyEnum;
import com.pig4cloud.pigx.common.customize.redis.utils.RedisUtils;
import com.pig4cloud.pigx.wallet.constant.RedisKeyPrefixConstant;
import com.pig4cloud.pigx.wallet.constant.WalletConstant;
import com.pig4cloud.pigx.wallet.entity.ImputationRecord;
import com.pig4cloud.pigx.wallet.entity.TimingConfig;
import com.pig4cloud.pigx.wallet.enums.TimingConfigEnums;
import com.pig4cloud.pigx.wallet.service.ImputationRecordService;
import com.pig4cloud.pigx.wallet.service.TimingConfigService;
import lombok.AllArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDateTime;

/**
 * 归集相关定时任务执行
 *
 */
@Component
@EnableScheduling
@AllArgsConstructor
public class TimingTask {

	private final TimingConfigService service;
	private final RedisUtils redisUtils;
	private final RedisTemplate redis;
	private final ImputationRecordService imputationRecordService;

	@Scheduled(fixedDelay = 200)
	public void serviceChargeTask() {
		for (TimingConfigEnums.Type type : TimingConfigEnums.Type.values()) {
			// 查看是否有需要执行的任务
			String key = RedisKeyPrefixConstant.TIMING_RUNNER_TIME + type.getValue();
			if (Boolean.FALSE.equals(redis.hasKey(key))){
				// 如果没有需要执行的任务的时间节点,则检查是否该任务已启用,已启用则设置执行节点
				TimingConfig config = (TimingConfig) redis.opsForHash().get(String.format("persistent_cache:%s","timing_config"), type.getValue());
				if (config == null || config.getStatus().equals(CommonDictEnum.Status.DEPRECATED.getValue())) {
					continue;
				}
				if (config.getStatus() != null && config.getStatus().equals(CommonDictEnum.Status.NORMAL.getValue())) {
					if (config.getTimingPeriod() != null && !config.getTimingPeriod().isEmpty()) {
						service.nextTimeChange(config);
					}
				}
				continue;
			}
			// 计算是否达到执行时间
			LocalDateTime now = LocalDateTime.now();
			LocalDateTime time = (LocalDateTime) redis.opsForValue().get(key);
			assert time != null;
			boolean nowAfter = now.isAfter(time);
			if (nowAfter) {
				redis.delete(key);
				// 获取配置信息
				TimingConfig config = (TimingConfig) redis.opsForHash().get(String.format("persistent_cache:%s","timing_config"), type.getValue());
				// 执行type对应的任务
				// 归集
				if (type.equals(TimingConfigEnums.Type.TRON_IMPUTATION)) {
					doImputation(config,WalletConstant.TRON);
				}
				if (type.equals(TimingConfigEnums.Type.ETH_IMPUTATION)) {
					doImputation(config,WalletConstant.ETH);
				}
				// 设置下一时间节点
				service.nextTimeChange(config);
			}
		}
	}

	/**
	 * 执行归集
	 * @param config 任务配置
	 */
	private void doImputation(TimingConfig config,String chain) {
		imputationRecordService.create(new ImputationRecord(){{
			setMainNetwork(chain);
			setCurrency(WalletConstant.USDT);
			setArrivalAddress(config.getAddress());
			setQuantity(BigDecimal.ONE);
		}});
	}

}
